<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>性格测试首页</title>
    <link rel="stylesheet" href="/layui/css/layui.css">
    <script src="/layui/layui.js"></script>
    <style type="text/css">
        html{
            height: 100%;
        }
        body{
            width: 100%;
            height: 100%;
            background-image: url("/image/k.png");
            background-repeat: no-repeat;
            background-size: 100% 100% ;
        }
        .container{
            width: 420px;
            height: 320px;
            min-height: 320px;
            max-height: 320px;
            position: absolute;
            top: 0;
            left: 0;
            bottom: 0;
            right: 0;
            margin: auto;
            padding: 20px;
            z-index: 130;
            border-radius: 8px;
            background-color: rgba(253, 255, 253, 0.6);
            box-shadow: 0 3px 18px rgba(100, 0, 0, .5);
            font-size: 16px;
        }
        .close{
            background-color: rgba(253, 255, 253, 0.6);
            border: none;
            font-size: 18px;
            margin-left: 410px;
            margin-top: -10px;
        }

        .layui-input{
            border-radius: 5px;
            width: 300px;
            height: 40px;
            font-size: 15px;
        }
        .layui-form-item{
            margin-left: -20px;
        }
        #logoid{
            margin-top: -16px;
            padding-left:150px;
            padding-bottom: 15px;
        }
        #testmg{
            height: 30px;
            font-size: 28px;
            font-family: 楷体;
        }
        .layui-btn{
            margin-left: -50px;
            border-radius: 5px;
            width: 350px;
            height: 40px;
            font-size: 15px;
        }
        .verity{
            width: 120px;
        }
        .font-set{
            font-size: 13px;
            text-decoration: none;
            margin-left: 120px;
        }
        a:hover{
            text-decoration: underline;
        }
.layui-btn{
    background-image: linear-gradient(to right, #e4afcb 0%, #b8cbb8 0%, #b8cbb8 0%, #e2c58b 30%, #c2ce9c 64%, #7edbdc 100%);
}
    </style>
</head>
<body>
<form class="layui-form">
    <div class="container">
        <button class="close" title="关闭">X</button>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <span id="testmg">性格测试问卷</span>
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">姓名</label>
            <div class="layui-input-block">
                <input type="text" name="name"   lay-verify="required|myName" placeholder="请输入用户名" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <label class="layui-form-label">手机号</label>
            <div class="layui-input-block">
                <input type="text" name="phone" lay-verify="required|phone|number|myPhone"   placeholder="请输入手机号" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-form-item">
            <div class="layui-input-block">
                <button class="layui-btn" lay-submit lay-filter="testFormFilter">开始测试</button>
            </div>
        </div>
    </div>
</form>
</body>
<script>
    layui.use(['form','layer','jquery'], function(){
        var form = layui.form;
        var layer = layui.layer;
        var $ = layui.jquery;
        //各种基于事件的操作，下面会有进一步介绍
        form.verify({
            myName: function(value, item){ //value：表单的值、item：表单的DOM对象
                if(!new RegExp("^[\u4E00-\u9FA5]{2,4}$").test(value)){
                    return '姓名只能是2到4位的汉字';
                }
            },
            myPhone:function(value){
                let flag = false;
                $.ajax({
                    type:"POST",
                    url:"/tester/findByPhone",  //指的是 对应后台的方法
                    // contentType:'application/json;charset=UTF-8',(后端参数里面有@ResposeBody时才使用）
                    async: false, //改为同步请求
                    data:{"phone": value},
                    dataType:'JSON',
                    success: function (data) {
                        if (data.code==0) {
                            //用户名存在
                            flag = true;
                        }
                    }
                });
                if(flag){
                    return '手机号已注册';
                }
            }
        });


        form.on('submit(testFormFilter)', function(data){
            console.log(data.field) //当前容器的全部表单字段，名值对形式：{name: value}
            //提交表单数据
            $.ajax({
                url:"/tester/insert",
                type: "POST",
                dataType:"JSON",
                contentType:"application/json;charset=UTF-8",
                data:JSON.stringify(data.field),
                success:function (ret) {
                    if(ret.code==0){
                        //提交成功之后，怎么办?
                        console.log(ret);
                        //window.location.href="ProblemTest";
                        window.location.href="/test/toTest";
                    }else
                    {
                        layer.msg(ret.msg,{icon: 5});
                    }

                }
            })
            return false; //阻止表单跳转。如果需要表单跳转，去掉这段即可。
        });
    });
</script>
</html>
